const PeopleService = require("../srevice/peopleService");

const peopleService = new PeopleService();

const { peopleNotFound, peopleFoundError, peopleCreateError } = require("../main/err.type")
class PeopleController {
    // 查询所有人员信息
    async findAll(ctx) {
        try {
            const res = await peopleService.findAll();
            let val = [];
            res.map((item) => {
                val.push(item.dataValues);
            });
            let sendMsg = {
                code: 200,
                data: val
            }
            ctx.body = sendMsg
            console.log(res.dataValues, val);
        } catch (error) {
            console.log(error);
            ctx.body = peopleFoundError;
            return ctx.app.emit("error", peopleFoundError, ctx);
        }
    }

    // 根据姓名查询人员信息
    async findByName(ctx) {
        try {
            const { name } = ctx.query;
            const res = await peopleService.findByName(name);
            console.log(res);
        } catch (error) {
            ctx.body = peopleFoundError;
            return ctx.app.emit("error", peopleFoundError, ctx);
        }
    }
    //分页查询
    async findPage(ctx) {
        const { page, size } = ctx.request.query
        console.log(page, size, 'page, size');
        const res = await peopleService.findPage(page, size);
        console.log(res);
        let val = [];
        res.rows.map((item) => {
            val.push(item.dataValues);
        });
        let sendMsg = {
            code: 200,
            count: res.count,
            limit: size * 1,
            page: page * 1,
            data: val
        }
        ctx.body = sendMsg

    }
    // 新增人员
    async createPeople(ctx) {
        try {
            const { name, sex, birth, phone, political, education, department, position, entryTime, status } = ctx.request.body
            console.log(name, sex, birth, phone, political, education, department, position, entryTime, status, 'name, sex, birth, phone, political, education, department, position, entryTime, statusname, sex, birth, phone, political, education, department, position, entryTime, status');
            const res = await peopleService.createPeopleService({ name, sex, birth, phone, political, education, department, position, entryTime, status });
            console.log(res.dataValues);
            ctx.body = {
                code: 200,
                message: '新增成功',
            }
        } catch (error) {
            console.log(error);
            ctx.body = peopleCreateError;
            return ctx.app.emit("error", peopleCreateError, ctx);
        }
    }
    // 删除人员
    async deletePeople(ctx) {
        try {
            // 先查找是否有这个人员
            const { id } = ctx.query;
            const res = await peopleService.findById(id);
            if (!res) {
                ctx.body = peopleNotFound;
                return ctx.app.emit("error", peopleNotFound, ctx);
            }
            const result = await peopleService.deletePeople(id);
            console.log(result);
            ctx.body = {
                code: 200,
                message: '删除成功'
            }

        } catch (error) {
            console.log(error);
            ctx.body = peopleNotFound;
            return ctx.app.emit("error", peopleNotFound, ctx);
        }
    }
}


module.exports = new PeopleController(); 